package com.epam.klymanov.tools;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.Polygon;

public class MapAreas {

	public static ArrayList<ArrayList<Spot>> getAreasWithSamePollution(
			PollutionMatrix pm, int beginPollution, int endPollution) {

		try {
			BufferedImage image = ImageIO
					.read(new File(
							"C:\\Documents and Settings\\Sasha\\workspace\\GreenPlanet\\pollution.jpg"));
			pm.readFromImage(image);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int matrix[][] = pm.getMatrix();
		AreaBuilder ab = new AreaBuilder(pm.getMatrix());

		ArrayList<ArrayList<Spot>> areas = new ArrayList<ArrayList<Spot>>();

		for (int i = 0; i < pm.getHeight(); i++) {
			for (int j = 0; j < pm.getWidth(); j++) {
				ArrayList<Spot> spots = ab.getArea(j, i, beginPollution,
						endPollution);
				areas.add(spots);
			}

		}

		while (areas.remove(null)) {

		}

		for (int i = 0; i < areas.size(); i++) {

			for (int j = 0; j < areas.size(); j++) {
				if ((areas.get(i) != null) && (areas.get(j) != null)
						&& (areas.get(i) != areas.get(j))
						&& areas.get(j).size() == areas.get(i).size()) {
					areas.set(j, null);
				}

			}
		}

		
		while (areas.remove(null)) {

		}

		return areas;
	}
}
